﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;

using System.Data.SqlClient;
using CNCSoft.EData;

using System.Media;
using Common;

namespace CNCSoft.ErpApps.TMS.Test
{
    public partial class frmThiTracNghiem : Form
    {
        SqlConnection conn = new SqlConnection(GlobalVariable.WSInfo.ConnectionString);
        DataSet ds = new DataSet();
        private string strEnquiryNumber = "";
        string _MaDeThi = "";
        int _ThoiGianLamBai = 0;

        public frmThiTracNghiem(string strMaDeThi, string strThoiGianLamBai)
        {
            InitializeComponent();
            _MaDeThi = strMaDeThi;
            _ThoiGianLamBai = Convert.ToInt16(strThoiGianLamBai);  //Tinh theo phut
        }

        /*
        public frmThiTracNghiem(string pEnquiryNumber)
        {
            InitializeComponent();
            strEnquiryNumber = pEnquiryNumber;
        }
        */

        private void frmThiTracNghiem_Load(object sender, EventArgs e)
        {
            lblKetQua.Text = "";
            lblRecommendedClass.Text = "";
            //txtEnquiryNumber.Text = strEnquiryNumber;
            lblName.Text = "";
            txtNgayThi.Text = GlobalVariable.WSInfo.WorkingDate.ToString("dd/MM/yyyy") ;
            
            AddQuestion();

            panelMain.Enabled = false;
            cmdStart.Enabled = false;
            cmdFinish.Enabled = false;

            timerTest.Stop();

        }

        private void AddQuestion()
        {
            this.SuspendLayout();

            SqlDataReader De_DataReader = SQLHelper.ExecuteReader(GlobalVariable.WSInfo.ConnectionString,
                                                      "SELECT * FROM TEST_Question WHERE MaDeThi = '" + _MaDeThi + "'");  //Placement test
            int STT = 0;
            int intGiaSoY = 0;

            //while (De_DataReader.Read() && STT < 5)
            while (De_DataReader.Read() )
            {
                try
                {
                    STT++;
                    int STT_CauHoi = Convert.ToInt32(De_DataReader["MaCauHoi"].ToString().Substring(2, 4));

                    if (STT_CauHoi == 1)
                    {
                        if (STT > 1)  STT++;     //Bat dau cac phan 2, phan 3, ... thi cong them 1 cho cach voi phan truoc
                        AddPhanGioiThieu(De_DataReader["MaCauHoi"].ToString().Substring(1, 1), (STT - 1) * 100 + 20);
                    }

                    try
                    {
                        intGiaSoY = Convert.ToInt32(De_DataReader["CachTrinhBay"]);
                    }
                    catch { intGiaSoY = 0; };
                    string cachtrinbay = De_DataReader["CachTrinhBay"].ToString();
                    if (De_DataReader["CachTrinhBay"].ToString() == "" || (De_DataReader["CachTrinhBay"].ToString().Substring(0, 1) != "R"
                        && De_DataReader["CachTrinhBay"].ToString().Substring(0,1) != "W"))
                        AddControlSingle(De_DataReader["MaCauHoi"].ToString(),
                                           (STT - 1) * 100 + 80 + intGiaSoY,
                                           STT_CauHoi.ToString("G") + ". " + De_DataReader["NoiDungCauHoi"].ToString().Trim(),
                                           De_DataReader["NoiDungCauA"].ToString().Trim(),
                                           De_DataReader["NoiDungCauB"].ToString().Trim(),
                                           De_DataReader["NoiDungCauC"].ToString().Trim(),
                                           De_DataReader["NoiDungCauD"].ToString().Trim(),
                                           De_DataReader["CachTrinhBay"].ToString().Trim());
                    else if(De_DataReader["CachTrinhBay"].ToString().Substring(0, 1) == "R" || De_DataReader["CachTrinhBay"].ToString().Substring(0, 1) == "W")
                        AddControlMultiReading(De_DataReader["MaCauHoi"].ToString(),
                                               (STT - 1) * 100 + 80,
                                               STT_CauHoi.ToString("G") + ". " + De_DataReader["NoiDungCauHoi"].ToString().Trim(),
                                               De_DataReader["NoiDungCauA"].ToString().Trim(),
                                               De_DataReader["NoiDungCauB"].ToString().Trim(),
                                               De_DataReader["NoiDungCauC"].ToString().Trim(),
                                               De_DataReader["NoiDungCauD"].ToString().Trim(),
                                               De_DataReader["CachTrinhBay"].ToString().Trim());
                }
                catch (Exception ex)
                {
                    MessageBox.Show(ex.Message);
                }
            }
        
            //Dong ket thuc
            AddPhanGioiThieu("E", STT*100 + 100 + intGiaSoY);

            this.ResumeLayout(false);
            this.PerformLayout();

        }

        private void AddControlSingle(string strMaCauHoi, int intY, string strNoidungCauhoi, string strA, string strB, string strC, string strD, string strCachTrinhBay )
        {
            Label lblNoiDungCauHoi = new Label();
            lblNoiDungCauHoi.AutoSize = false;
            lblNoiDungCauHoi.Width = this.Width - 50;
            lblNoiDungCauHoi.Height = 32; //2 cau
            lblNoiDungCauHoi.Location = new System.Drawing.Point(40, intY);
            //lblNoiDungCauHoi.Font = new Font("Arial", 10);
            //if (strCachTrinhBay.Length >0 &&  strCachTrinhBay.Substring(0, 1) == "B")
            //    lblNoiDungCauHoi.Font = new Font(lblNoiDungCauHoi.Font, FontStyle.Bold); 
            lblNoiDungCauHoi.Name = strMaCauHoi;
            lblNoiDungCauHoi.TabIndex = 0;
            lblNoiDungCauHoi.Text = strNoidungCauhoi; 
            this.panelMain.Controls.Add(lblNoiDungCauHoi);

            if (strA + strB + strC + strD == "") return;

            //Group A/B/C/D
            //GroupBox grpABCD = new GroupBox();
            Panel grpABCD = new Panel();
            grpABCD.AutoSize = true;
            grpABCD.Location = new Point(lblNoiDungCauHoi.Location.X + 10, lblNoiDungCauHoi.Location.Y + 10);
            grpABCD.AutoSizeMode = AutoSizeMode.GrowAndShrink;
            grpABCD.Name = "grp" + strMaCauHoi;
            grpABCD.Tag = "";
            this.panelMain.Controls.Add(grpABCD);

            //Noi dung tung cau A  (truoc mat cho TH single choise)
            RadioButton NoiDungCauA = new RadioButton();
            NoiDungCauA.AutoSize = true;
            NoiDungCauA.Location = new Point(10, 20);
            NoiDungCauA.Text = "a. " + strA;
            NoiDungCauA.Name = "A_" + strMaCauHoi;
            if (strA != "")
                this.panelMain.Controls["grp" + strMaCauHoi].Controls.Add(NoiDungCauA);
            NoiDungCauA.CheckedChanged += new System.EventHandler(radioButtonABCD_CheckedChanged);

            //Noi dung tung cau B 
            RadioButton NoiDungCauB = new RadioButton();
            NoiDungCauB.AutoSize = true;
            //NoiDungCauB.Location = new Point(200, NoiDungCauA.Location.Y);
            NoiDungCauB.Location = new Point(400, NoiDungCauA.Location.Y);
            NoiDungCauB.Text = "b. " + strB;
            NoiDungCauB.Name = "B_" + strMaCauHoi;
            if (strB != "")
                this.panelMain.Controls["grp" + strMaCauHoi].Controls.Add(NoiDungCauB);
            NoiDungCauB.CheckedChanged += new System.EventHandler(radioButtonABCD_CheckedChanged);

            //Noi dung tung cau C
            RadioButton NoiDungCauC = new RadioButton();
            NoiDungCauC.AutoSize = true;
            //NoiDungCauC.Location = new Point(400, NoiDungCauA.Location.Y);
            NoiDungCauC.Location = new Point(800, NoiDungCauA.Location.Y);
            NoiDungCauC.Text = "c. " + strC;
            NoiDungCauC.Name = "C_" + strMaCauHoi;
            if (strC != "")
                this.panelMain.Controls["grp" + strMaCauHoi].Controls.Add(NoiDungCauC);
            NoiDungCauC.CheckedChanged += new System.EventHandler(this.radioButtonABCD_CheckedChanged);

            //Noi dung tung cau D
            RadioButton NoiDungCauD = new RadioButton();
            NoiDungCauD.AutoSize = true;
            //NoiDungCauD.Location = new Point(550, NoiDungCauA.Location.Y);
            NoiDungCauD.Location = new Point(1200, NoiDungCauA.Location.Y);
            NoiDungCauD.Text = "d. " + strD;
            NoiDungCauD.Name = "D_" + strMaCauHoi;
            if (strD != "")
                this.panelMain.Controls["grp" + strMaCauHoi].Controls.Add(NoiDungCauD);
            NoiDungCauD.CheckedChanged += new System.EventHandler(this.radioButtonABCD_CheckedChanged);
        }

        private void AddControlMultiReading(string strMaCauHoi, int intY, string strNoidungCauhoi, string strA, string strB, string strC, string strD, string strCachTrinhBay)
        {
            int intVitriABCD = 0;
            //Phan noi dung bai doc ben trai
            if (strCachTrinhBay == "R0")  //Chi add panel cho cau lien tuc dau tien
            {
                panelBaiDoc.Visible = true;
                //-----------longnt----------------------
                panelBaiDoc.Width = 800;
                //panelBaiDoc.Height = 1000;
                //-----------endlongnt-------------------
                panelBaiDoc.Top = intY;
                panelBaiDoc.Left = 40;
                intVitriABCD = intY - 10;
                //richTextBoxBaiDoc.LoadFile(Application.StartupPath +  "\\English\\PlacementReading" + _MaDeThi + ".rtf");
                richTextBoxBaiDoc.LoadFile(Environment.CurrentDirectory + "\\PlacementReading" + _MaDeThi + ".rtf");
            }
            else
            {
                intVitriABCD = panelBaiDoc.Top - 10 + (Convert.ToInt32(strCachTrinhBay.Substring(1))) * 32;
            }
            panelBaiDoc.Height = intVitriABCD - panelBaiDoc.Top + 500 ;  //intY - panelBaiDoc.Top; //panelBaiDoc.Height + 30;
            //txtNoiDungReading.Height = panelBaiDoc.Height;
            //richTextBoxBaiDoc.rt
            //richTextBoxBaiDoc.Text = richTextBoxBaiDoc.Text + strNoidungCauhoi.Substring(3);
            //richTextBoxBaiDoc.LoadFile("C:\\PlacementReading.rtf");
            

            
            
            
            //-------------------longnt----------------------------
            
            if (strCachTrinhBay.Substring(0, 1).ToLower() == "w")
            {

                //Phan textbox o duoi panelbaidoc
                //GroupBox grpABCD = new GroupBox();
                Panel grpABCD = new Panel();
                grpABCD.AutoSize = true;
                grpABCD.Location = new Point(40, panelBaiDoc.Bottom + 300);
                grpABCD.AutoSizeMode = AutoSizeMode.GrowAndShrink;
                grpABCD.Name = "grp" + strMaCauHoi;
                grpABCD.Tag = "";
                this.panelMain.Controls.Add(grpABCD);
                //Label so cau hoi truoc A/B/C/D
                Label labelSoCauHoi = new Label();
                labelSoCauHoi.Location = new Point(10, 22);
                labelSoCauHoi.AutoSize = true;
                labelSoCauHoi.Text = strNoidungCauhoi;
                this.panelMain.Controls["grp" + strMaCauHoi].Controls.Add(labelSoCauHoi);
                //---------------------textbox------------------------------------------------------
                TextBox Writing = new TextBox();
                Writing.Width = 200;
                Writing.Height = 32;
                Writing.Location = new Point(labelSoCauHoi.PreferredWidth + 10, 20);
                Writing.Text = "";
                Writing.Name = "--" + strMaCauHoi;
                this.panelMain.Controls["grp" + strMaCauHoi].Controls.Add(Writing);
                Writing.TextChanged += new System.EventHandler(textBoxWriting_TextChanged);
            }
            
            //-------------------endlongnt-------------------------
            else
            {

                //Phan A/B/C/D ben phai
                //GroupBox grpABCD = new GroupBox();
                Panel grpABCD = new Panel();
                grpABCD.AutoSize = true;
                grpABCD.Location = new Point(panelBaiDoc.Left + panelBaiDoc.Width + 20, intVitriABCD + 10);
                grpABCD.AutoSizeMode = AutoSizeMode.GrowAndShrink;
                grpABCD.Name = "grp" + strMaCauHoi;
                grpABCD.Tag = "";
                this.panelMain.Controls.Add(grpABCD);
                //Label so cau hoi truoc A/B/C/D
                Label labelSoCauHoi = new Label();
                labelSoCauHoi.Location = new Point(10, 22);
                labelSoCauHoi.AutoSize = true;
                labelSoCauHoi.Text = strNoidungCauhoi.Substring(0, 3);
                this.panelMain.Controls["grp" + strMaCauHoi].Controls.Add(labelSoCauHoi);
                //Noi dung tung cau A  (truoc mat cho TH single choise)
                RadioButton NoiDungCauA = new RadioButton();
                NoiDungCauA.AutoSize = true;
                NoiDungCauA.Location = new Point(40, 20);
                if (strA.ToLower() != "a")
                {
                    NoiDungCauA.Text = "a. " + strA;
                }
                else
                {
                    NoiDungCauA.Text = "a.";
                }
                NoiDungCauA.Name = "A_" + strMaCauHoi;
                if (strA != "")
                    this.panelMain.Controls["grp" + strMaCauHoi].Controls.Add(NoiDungCauA);
                NoiDungCauA.CheckedChanged += new System.EventHandler(radioButtonABCD_CheckedChanged);

                //Noi dung tung cau B 
                RadioButton NoiDungCauB = new RadioButton();
                NoiDungCauB.AutoSize = true;
                NoiDungCauB.Location = new Point(160, NoiDungCauA.Location.Y);
                if (strB.ToLower() != "b")
                {
                    NoiDungCauB.Text = "b. " + strB;
                }
                else
                {
                    NoiDungCauB.Text = "b.";
                }
                NoiDungCauB.Name = "B_" + strMaCauHoi;
                if (strB != "")
                    this.panelMain.Controls["grp" + strMaCauHoi].Controls.Add(NoiDungCauB);
                NoiDungCauB.CheckedChanged += new System.EventHandler(radioButtonABCD_CheckedChanged);

                //Noi dung tung cau C 
                RadioButton NoiDungCauC = new RadioButton();
                NoiDungCauC.AutoSize = true;
                NoiDungCauC.Location = new Point(300, NoiDungCauA.Location.Y);
                if (strC.ToLower() != "c")
                {
                    NoiDungCauC.Text = "c. " + strC;
                }
                else
                {
                    NoiDungCauC.Text = "c.";
                }
                NoiDungCauC.Name = "C_" + strMaCauHoi;
                if (strC != "")
                    this.panelMain.Controls["grp" + strMaCauHoi].Controls.Add(NoiDungCauC);
                NoiDungCauC.CheckedChanged += new System.EventHandler(radioButtonABCD_CheckedChanged);
            }
        }

        private void AddPhanGioiThieu(string Theloai, int intY)
        {
            Label lblNoiDungCauHoi = new Label();
            lblNoiDungCauHoi.AutoSize = false;
            lblNoiDungCauHoi.Width = this.Width - 200;
            lblNoiDungCauHoi.Height = 32; //2 cau
            lblNoiDungCauHoi.Location = new System.Drawing.Point(40, intY);
            lblNoiDungCauHoi.Font = new Font("Arial", 12);
            lblNoiDungCauHoi.Font = new Font(lblNoiDungCauHoi.Font, FontStyle.Bold);
            lblNoiDungCauHoi.Text =  GlobalVariable.WSInfo.getAppProperty("English.Intro_" + Theloai,"");
            this.panelMain.Controls.Add(lblNoiDungCauHoi);
        }

        private void cmdFinish_Click(object sender, EventArgs e)
        {
            timerTest.Stop();
            axWindowsMediaPlayer1.Ctlcontrols.stop();

            cmdFinish.Enabled = false;
            txtEnquiryNumber.ReadOnly = true;
            txtNgayThi.ReadOnly = true;

            //Tinh tong diem
            //Update Diem, Ngay Thi, Diem thanh phan, recommended, ... vao table Enquiry (thong qua eqn number)

            //Tinh tong so diem
            SqlDataReader De_DataReader = SQLHelper.ExecuteReader(GlobalVariable.WSInfo.ConnectionString,
                        "SELECT * FROM TEST_Question WHERE left(MaCauHoi,1) = 'P' AND MaDeThi = '" + _MaDeThi + "'");  //Placement test
            int STT = 0;
            int intCorrectReading = 0;
            int intCorrectListening = 0;
            //while (De_DataReader.Read() && STT < 5)
            while (De_DataReader.Read())
            {
                try
                {
                    STT++;
                    string strMaCauHoi = De_DataReader["MaCauHoi"].ToString();
                    string strDapAn = De_DataReader["DapAn"].ToString().ToLower();
                    string strTheLoaiCauHoi = strMaCauHoi.Substring(1, 1);
                    /*
                    MessageBox.Show("Cau: " + this.panelMain.Controls["grp" + strMaCauHoi].Name +
                        "\nTra loi: " + this.panelMain.Controls["grp" + strMaCauHoi].Tag.ToString() +
                        "\nDap an: " + strDapAn) ;
                    */
                    if ( strTheLoaiCauHoi == "L" && this.panelMain.Controls["grp" + strMaCauHoi].Tag.ToString().ToLower() == strDapAn)
                        intCorrectListening++;
                    if (strTheLoaiCauHoi == "R" && this.panelMain.Controls["grp" + strMaCauHoi].Tag.ToString().ToLower() == strDapAn)
                        intCorrectReading++;
                    //-----------longnt-----------------------------------
                    //Insert to ResultPlacementTest
                    //LoadResultPlacementTestByEnquiryNumberAndMaDeThi(txtEnquiryNumber.Text, De_DataReader["MaDeThi"].ToString());
                    
                    InsertToResultPlacementTest(txtEnquiryNumber.Text, strMaCauHoi, De_DataReader["NoiDungCauHoi"].ToString(), De_DataReader["NoiDungCauA"].ToString(), De_DataReader["NoiDungCauB"].ToString(),
                                                De_DataReader["NoiDungCauC"].ToString(), De_DataReader["NoiDungCauD"].ToString(), De_DataReader["CachTrinhBay"].ToString(),
                                                this.panelMain.Controls["grp" + strMaCauHoi].Tag.ToString(), De_DataReader["DapAn"].ToString(), De_DataReader["MaDeThi"].ToString());
                    
                    //-----------endlongnt--------------------------------
                }
                catch (Exception ex)
                {
                    MessageBox.Show(ex.Message);
                }
            } //End While

            lblKetQua.Text = "Listening:  " + intCorrectListening.ToString("G") + "  +  Reading:  " + intCorrectReading.ToString("G") +
                             "  =>  Total:  " + (intCorrectListening + intCorrectReading).ToString("G");
            lblRecommendedClass.Text = ""; //Tu van tu quyet// "Recommended class: " + Return_RecommendedTest(intCorrectListening + intCorrectReading);
            string strRecommendedClass = "Recommended class: " + Return_RecommendedTest(intCorrectListening + intCorrectReading);

            //Luu TEST_Result
            try
            {
                //Delete
                SqlDataReader TestResultDelete_DataReader = SQLHelper.ExecuteReader(GlobalVariable.WSInfo.ConnectionString,
                                                          "DELETE FROM TEST_Result " +
                                                          "WHERE EnquiryNumber = '" + txtEnquiryNumber.Text  + "' " +
                                                          "AND TestDate = Convert(Datetime, '" + txtNgayThi.Text + "', 103)"
                                                           );
                //Insert
                SqlDataReader TestResultInsert_DataReader = SQLHelper.ExecuteReader(GlobalVariable.WSInfo.ConnectionString,
                                                          "INSERT INTO [TEST_Result] ([EnquiryNumber],[TestDate],[ScoreListening],[ScoreReading],[RecommendedClass]) " +
                                                          "VALUES ('" + txtEnquiryNumber.Text + "', Convert(Datetime, '" + txtNgayThi.Text + "', 103), " + intCorrectListening + ", " + intCorrectReading + ", '" +
                                                          strRecommendedClass.Substring(19).Trim() + "') "
                                                           );
            }
            catch (Exception ex)
            {
                //MessageBox.Show(ex.Message);
            }
        }

        private string Return_RecommendedTest(int TongsoDiem)
        {
            if(TongsoDiem <=70)
                return "GE - Intro (Beginner)";
            else if(TongsoDiem <=80)
                return "GE - English 1 (Beginner)";
            else if (TongsoDiem <= 90)
                return "GE - English 2 (Elementary)";
            else if (TongsoDiem <= 100)
                return "GE - English 3 (Elementary)";
            else if (TongsoDiem <= 120)
                return "GE - English 4 (Pre-Intermediate)";
            else if (TongsoDiem <= 130)
                return "GE - English 5 (Pre-Intermediate)";
            else if (TongsoDiem <= 140)
                return "GE - English 6 (Intermediate)";
            else if (TongsoDiem <= 150)
                return "GE - English 7 (Intermediate)";
            else if (TongsoDiem <= 160)
                return "GE - English 8 (Upper-Intermediate)";
            else if (TongsoDiem <= 200)
                return "GE - English 9 (Upper-Intermediate)";
            else
                return "";
        }

        private void radioButtonABCD_CheckedChanged(object sender, EventArgs e)
        {
            try
            {
                string strMaCauHoi = ((RadioButton)sender).Name.ToString().Substring(2);
                //MessageBox.Show(((RadioButton)sender).Name + " MCH: " + strMaCauHoi);
                //MessageBox.Show(((RadioButton)sender).Name.ToString().Substring(0, 1));
                this.panelMain.Controls["grp" + strMaCauHoi].Tag = ((RadioButton)sender).Name.ToString().Substring(0, 1);  //A,B,C,D
                string tag = this.panelMain.Controls["grp" + strMaCauHoi].Tag.ToString();
            }
            catch (Exception ex)
            {
                //MessageBox.Show(ex.Message);
            }
            
        }

        private void textBoxWriting_TextChanged(object sender, EventArgs e)
        {
            try
            {
                string strMaCauHoi = ((TextBox)sender).Name.ToString().Substring(2);
                this.panelMain.Controls["grp" + strMaCauHoi].Tag = ((TextBox)sender).Text;
                string tag = this.panelMain.Controls["grp" + strMaCauHoi].Tag.ToString();
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }

        private void txtEnquiryNumber_TextChanged(object sender, EventArgs e)
        {
            if (txtEnquiryNumber.Text == "") return;
            try
            {
                lblName.Text = SQLHelper.ExecuteScalar(GlobalVariable.WSInfo.ConnectionString,
                                        "SELECT Holot + ' ' + Ten as HoTenHV " +
                                        "FROM Enquiry " +
                                        "WHERE EnquiryNumber  = '" + txtEnquiryNumber.Text + "'").ToString().ToUpper();
            }
            catch { lblName.Text = ""; }

            cmdStart.Enabled = true;
        }


        private void cmdStart_Click(object sender, EventArgs e)
        {
            //-------longnt----------------------------------
            LoadResultPlacementTestByEnquiryNumberAndMaDeThi(txtEnquiryNumber.Text, _MaDeThi);
            if (ds.Tables["TableResultByEnquiryNumberAndMaDeThi"].Rows.Count > 0)
            {
                MessageBox.Show("bạn đã từng làm đề này");
                return;
            }
            else
            {
                //-------endlongnt-------------------------------
                panelMain.Enabled = true;
                cmdFinish.Enabled = true;
            }
            
            //Xac dinh thoi gian (phut)
            /*
            progressBarTime.Maximum = _ThoiGianLamBai; //60 (phut)
            progressBarTime.Value = progressBarTime.Maximum;
            lblTimeLeft.Text = "Time left (minutes): " + progressBarTime.Value.ToString();

            timerTest.Interval = _ThoiGianLamBai * 1000; //60000; //(1 phut)
            timerTest.Start();  //Bat dau dem gio

            cmdStart.Enabled = false;

            axWindowsMediaPlayer1.URL = GlobalVariable.WSInfo.ApplicationStartupPath + "\\English\\Placement_Listening.m4a";
             */

            /*
            try
            {
                simpleSound = new SoundPlayer(GlobalVariable.WSInfo.ApplicationStartupPath + "\\English\\Placement_Listening.wav");
                simpleSound.Play();
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
            */
            
        }

        private void timerTest_Tick(object sender, EventArgs e)
        {
            if (progressBarTime.Value == 0)
            {
                cmdFinish_Click(sender, e); 
                MessageBox.Show("Test time has expired !", "Sorry");

                return;
            }
            // mot phut troi qua
            progressBarTime.Value--;
            lblTimeLeft.Text = "Time left (minutes): " + progressBarTime.Value.ToString() ;

            //lblRecommendedClass.Text = progressBarTime.Value.ToString("G");
        }

        private void frmThiTracNghiem_FormClosing(object sender, FormClosingEventArgs e)
        {
            if (cmdFinish.Enabled)
            {
                MessageBox.Show("Click <Finish button> to finish the test before closing form", "Placement Test", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
                e.Cancel = true;
            }
        }

        private void chkDisplayTimeLeft_CheckedChanged(object sender, EventArgs e)
        {
            lblTimeLeft.Visible = chkDisplayTimeLeft.Checked;
        }

        //longnt----------------------
        private void InsertToResultPlacementTest(string enquirynumber, string macauhoi, string noidungcauhoi, string noidunga, string noidungb, string noidungc, string noidungd,
                                                   string cachtrinhbay, string traloi, string dapan, string madethi)
        {
            try
            {
                string sql = @"usp_InsertResultPlacementTest @enquirynumber = @enqnum, @macauhoi = @cauhoi, @noidungcauhoi = @noidung, @noidungcaua = @noidunga, @noidungcaub = @noidungb,
                                                                @noidungcauc = @noidungc, @noidungcaud = @noidungd, @cachtrinhbay = @trinhbay, @traloi = @traloi, @dapan = @dapan, @madethi = @dethi";
                SqlCommand cmd = new SqlCommand(sql, conn);
                cmd.Parameters.AddWithValue("@enqnum", enquirynumber);
                cmd.Parameters.AddWithValue("@cauhoi", macauhoi);
                cmd.Parameters.AddWithValue("@noidung", noidungcauhoi);
                cmd.Parameters.AddWithValue("@noidunga", noidunga);
                cmd.Parameters.AddWithValue("@noidungb", noidungb);
                cmd.Parameters.AddWithValue("@noidungc", noidungc);
                cmd.Parameters.AddWithValue("@noidungd", noidungd);
                cmd.Parameters.AddWithValue("@trinhbay", cachtrinhbay);
                cmd.Parameters.AddWithValue("@traloi", traloi);
                cmd.Parameters.AddWithValue("@dapan", dapan);
                cmd.Parameters.AddWithValue("@dethi", madethi);
                int i = 0;
                try
                {
                    conn.Open();
                    i = cmd.ExecuteNonQuery();
                }
                finally
                {
                    if (conn != null)
                        conn.Close();
                }
                if (i > 0)
                {
                    //MessageBox.Show("Save bài làm thành công");
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }

        private void DeleteResultPlacementTestByEnquiryNumberAndMaDeThi(string enquirynumber, string madethi)
        {
            try
            {
                string sql = @"usp_DeleteResultPlacementTestByEnquiryNumberAndMaDeThi @enquirynumber = @enqnum, @madethi = @madethi";
                SqlCommand cmd = new SqlCommand(sql, conn);
                cmd.Parameters.AddWithValue("@enqnum", enquirynumber);
                cmd.Parameters.AddWithValue("@madethi", madethi);
                int i = 0;
                try
                {
                    conn.Open();
                    i = cmd.ExecuteNonQuery();
                }
                finally
                {
                    if (conn != null)
                        conn.Close();
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }

        private void LoadResultPlacementTestByEnquiryNumberAndMaDeThi(string enquirynumber, string madethi)
        {
            try
            {
                if (ds.Tables["TableResultByEnquiryNumberAndMaDeThi"] != null)
                    ds.Tables["TableResultByEnquiryNumberAndMaDeThi"].Clear();
                string sql = @"usp_AllResultPlacementTestByEnquiryAndMaDeThi @enquirynumber = '" + enquirynumber + "', @madethi = '" + @madethi +"'";
                SqlDataAdapter da = new SqlDataAdapter(sql, conn);
                da.Fill(ds, "TableResultByEnquiryNumberAndMaDeThi");
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }
        //endlongnt-------------------
    }
}
